import request from '@/utils/request';

function PromiseU(o){
    return new Promise((resolve, reject) => {
        resolve(o);
    });
}

export function rGetBussinessUsers(){
    return PromiseU({
        code: 0,
        data: ['user1', 'user2', 'user3', 'user4',]
    });
}

export function rGetBussinessRules(){
    return request({url: '/bs/getRules', method: 'get'});
}

export function rGetBussinessRecords(lq){

    /*
      const l = [
      {"id": 1, "time": 1733207632469, "from": "u1", "to": "u2", "type": "READ", "cmd": "发送短信", "auditResult": "true", "auditReason": ""},
      {"id": 2, "time": 1733207632469, "from": "u2", "to": "u1", "type": "READ", "cmd": "根据收到的短信回复短信", "auditResult": "false", "auditReason": "不合法"},
      {"id": 3, "time": 1733207632469, "from": "u3", "to": "u2", "type": "READ", "cmd":  "根据收到的回复短信回复短信", "auditResult": "true", "auditReason": ""},
      {"id": 4, "time": 1733207632469, "from": "u3", "to": "u2", "type": "READ", "cmd":  "根据收到的回复短信回复短信", "auditResult": "true", "auditReason": ""},
      ];
      return PromiseU(
      {code: 0, data: {voList: l, totalCount: 4, totalPages: 1}}
      );
    */
    // --------------------------------------------------

    return request({url: '/bs/getRecords', method: 'get',
                    params: {
                        pageNum: lq.page,
                        pageSize: lq.limit,
                        from: lq.from,
                        eventType: lq.eventType,
                        sinceTime: lq.sinceTime,
                        untilTime: lq.untilTime,
                        sortBy : lq.sort,
                        asc: lq.ascending
                    }
                   });

}

export function rStartBussinessAudit(rules){
    // return PromiseU({code: 0, data: 10});
    return request({url: '/bs/audit', method: 'post',
                    data: rules});
}


export function rStartAudit(p){
    // return PromiseU({code: 0, data: 10});
    return request({url: '/cmd/startAudit', method: 'get',
                    params: {
                        type: p.type,
                        url: p.url,
                        gk: p.gk,
                        nb: p.Nb,

                    }
                   });
}

export function rAuditPerc(){
    // return PromiseU({code: 0, data: 10});
    return request({url: '/cmd/percAudit', method: 'get'});
}

export function rAuditAbort(){
    // return PromiseU({code: 0, data: 10});
    return request({url: '/cmd/abortAudit', method: 'get'});
}

export function rIsStreaming(){
    // return PromiseU({
    //     code: 0,
    //     data: true
    // });
    return request({url: '/cmd/isUp', method: 'get'});
}
export function rToggle(){
    return request({url: '/cmd/toggle', method: 'get'});
}

export function fetchPk(){
    return request({url: '/cmd/pk', method: 'get'});
}

export function rFold(){
    // return PromiseU({code: 0, data: 10});
    return request({url: '/cmd/fold', method: 'get',
                    timeout: Number.MAX_SAFE_INTEGER});
    // 100_000_000});
}

export function rRegister(){
    // return PromiseU({code: 0, data: 10});
    return request({url: '/cmd/reg', method: 'get', timeout: 60000});
}

export function fetchFs(lq){
    return request({
        url: '/fs/get', method: 'get',
        params: {
            pageNum: lq.page,
            pageSize: lq.limit,
            user: lq.user,
            eventType: lq.eventType,
            fileName: lq.fileName,
            sinceTime: lq.sinceTime,
            untilTime: lq.untilTime,
            hash: lq.hash,
            sortBy : lq.sort,
            asc: lq.ascending
        }
    });
}

export function fetchUsers() {
  return request({url: '/basic/getUsers', method: 'get'});
}

// 2000,Jan,1,02:03:04:005 = new Date(2000,0,1,2,3,4)
const mockedCmds = [
    {
        id: 1, time: 1731487961870, cmd: "ls-mocked",
        pid: 0, ppid: 1, uid: 2,
        argv: "ls-mocked -l /home/me/Music/black-footed-cat-purr.mp3",
        retVal: 0, hash: ""
    },
    {
        id: 2, time: 1731488081949,
        cmd: "ls-mocked", pid: 3,
        ppid: 4, uid: 5,
        argv: "ls-mocked -l /home/me/Music/black-footed-cat-purr.mp3",
        retVal: 0, hash: ""
    },
    {
        id: 3, time: 1731488201959,
        cmd: "ls-mocked", pid: 6,
        ppid: 7, uid: 8,
        argv: "ls-mocked -l /home/me/Music/black-footed-cat-purr.mp3",
        retVal: 0, hash: ""
    },
    {
        id: 4, time: 1731488322059,
        cmd: "ls-mocked", pid: 9,
        ppid: 10, uid: 11,
        argv: "ls-mocked -l /home/me/Music/black-footed-cat-purr.mp3",
        retVal: 0, hash: ""
    },
    {
        id: 5, time: 1731488442140,
        cmd: "ls-mocked", pid: 12,
        ppid: 13, uid: 14,
        argv: "ls-mocked -l /home/me/Music/black-footed-cat-purr.mp3",
        retVal: 0, hash: ""
    }
];

function sleep(sec){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, sec * 1000);
    });
}


/*
  interface ListQuery {
  page: number; limit: number; user: string | undefined;
  cmd: string | undefined; hash: string | undefined; sort: string;
  ascending: boolean;
  }
*/
export function fetchCmds(lq){
    // mock
    // --------------------------------------------------
    // return PromiseU({
    //     code: 0,
    //     data: {
    //         voList: mockedCmds,
    //         totalCount: 4,
    //         totalPages: 3,
    //     }
    // });

    // real
    // --------------------------------------------------
    // http://localhost:8081/cmd/get?pageNum=1&pageSize=2&uid=0&cmd=sh&sinceTime=2021-01-01%2000%3A00%3A00&untilTime=2021-01-01%2000%3A00%3A00&hash=aabbcc&sortBy=time (example)

    /*
     */

    return request({url: '/cmd/get', method: 'get',
                    params: {
                        pageNum: lq.page,
                        pageSize: lq.limit,
                        uid: lq.uid,
                        cmd: lq.cmd,
                        sinceTime: lq.sinceTime,
                        untilTime: lq.untilTime,
                        hash: lq.hash,
                        sortBy : lq.sort,
                        asc: lq.ascending
                    }
                   });
}

const mockedNodeInfo = {
    "cnsss": "listenToOne",
    "is_primary": true,
    "listened_endpoint": {
        "pk": "<mock-pk>",
        "addr_port": "localhost:7777",
        "cert": ""
    },
    "known_subs": [],
    "txs_count": 1,
    "latest_blk": {
        "number": 0,
        "hash": "3cb681d659097c833465914c3591ee770994de0ca66a038facd538b8c0e4c354",
        "parentHash": "0000000000000000000000000000000000000000000000000000000000000000",
        "txs": [
            {
                "from": "0000000000000000000000000000000000000001",
                "to": "0000000000000000000000000000000000000000",
                "nonce": 123,
                "timestamp": 1731728678,
                "hash": "b06b89b665df6f7ff1be967faf9a0601f71c0a3cdb8250c48fe7fcc663b18d1b",
                "type": "data",
                "data": "This is the initial block"
            }
        ],
        "stateChanges": [
            []
        ],
        "txReceipts": [
            {
                "ok": true,
                "result": "",
                "type": "data"
            }
        ]
    }
};

export function fetchNodeInfo(url){
    // mock
    // --------------------------------------------------
    // return PromiseU({
    //     code: 0,
    //     data: mockedNodeInfo
    // });

    // real
    // --------------------------------------------------
    return request({url: '/chain/getNodeStatus', method: 'get', params: {url} });
}
